package top.continew.admin.generator.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import top.continew.admin.system.model.req.OptionReq;
import top.continew.starter.extension.crud.annotation.CrudApi;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.generator.model.query.ProvinceLeadersQuery;
import top.continew.admin.generator.model.req.ProvinceLeadersReq;
import top.continew.admin.generator.model.resp.ProvinceLeadersDetailResp;
import top.continew.admin.generator.model.resp.ProvinceLeadersResp;
import top.continew.admin.generator.service.ProvinceLeadersService;
import top.continew.admin.generator.model.entity.ProvinceLeadersDO;
import top.continew.starter.extension.crud.model.resp.IdResp;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.validation.CrudValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Collections;
import java.util.List;

/**
 * 省区负责人管理 API
 *
 * @author jiangjun
 * @since 2025/06/24 10:40
 */
@Tag(name = "省区负责人管理 API")
@RestController
@CrudRequestMapping(value = "/generator/provinceLeaders", api = {Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class ProvinceLeadersController extends BaseController<ProvinceLeadersService, ProvinceLeadersResp, ProvinceLeadersDetailResp, ProvinceLeadersQuery, ProvinceLeadersReq> {

    @Autowired
    private ProvinceLeadersService service;

    @Operation(summary = "分页查询省区负责人列表（包含认证状态）")
    @GetMapping
    public PageResp<ProvinceLeadersResp> page(@Valid ProvinceLeadersQuery query) {
        Page<ProvinceLeadersDO> page = new Page<>(query.getPage(), query.getSize());
        return service.pageWithAuth(query, page);
    }
    @Operation(summary = "删除省区负责人")
    @DeleteMapping({"/{id}"})
    public void delete(@PathVariable("id") Long id) {
        service.delete(Collections.singletonList(id));
    }


    public IdResp<Long> create(@Validated(CrudValidationGroup.Create.class) @RequestBody ProvinceLeadersReq req) {
        return service.save(req);
    }

    @CrudApi(Api.UPDATE)
    @Operation(
            summary = "修改数据",
            description = "修改数据"
    )
    @Parameter(
            name = "id",
            description = "ID",
            example = "1",
            in = ParameterIn.PATH
    )
    @ResponseBody
    @PutMapping({"/{id}"})
    public void update(@Validated({CrudValidationGroup.Update.class}) @RequestBody ProvinceLeadersReq req, @PathVariable("id") Long id) {
        service.updateCustom(req, id);
    }

    @Operation(summary = "获取所有负责人列表")
    @GetMapping("/all")
    public List<OptionReq> getAllLeaders(@RequestParam(required = false) String keyword) {
        return service.getAllLeaders(keyword);
    }
}